#include "clockstatslotmodel.h"

ClockStatSlotModel::ClockStatSlotModel(QObject *parent ) : QAbstractListModel(parent)
{

}

int ClockStatSlotModel::rowCount(const QModelIndex &) const
{
    return m_contacts.count();
}

QVariant ClockStatSlotModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < rowCount())
        switch (role) {
        case DateRole: return m_contacts.at(index.row()).date;
        case TimeRole: return m_contacts.at(index.row()).time;
        case PlaceRole: return m_contacts.at(index.row()).place;
        case IsClockOnRole: return m_contacts.at(index.row()).isClockOn;
        default: return QVariant();
        }
    return QVariant();
}

QHash<int, QByteArray> ClockStatSlotModel::roleNames() const
{
    static const QHash<int, QByteArray> roles {
        { DateRole, "date" },
        { TimeRole, "time" },
        { PlaceRole, "place" },
        { IsClockOnRole, "isClockOn" }
    };
    return roles;
}

QVariantMap ClockStatSlotModel::get(int row) const
{
    const Contact contact = m_contacts.value(row);
    return { {"date", contact.date}, {"time", contact.time}, {"place", contact.place}, {"isClockOn", contact.isClockOn} };
}

void ClockStatSlotModel::append(const Contact contacts)
{
    int row = 0;
    while (row < m_contacts.count())
        ++row;
    beginInsertRows(QModelIndex(), row, row);
    m_contacts.insert(row, contacts);
    endInsertRows();
}


void ClockStatSlotModel::remove(int row)
{
    if (row < 0 || row >= m_contacts.count())
        return;

    beginRemoveRows(QModelIndex(), row, row);
    m_contacts.removeAt(row);
    endRemoveRows();
}
